<?php
/**
 * 商品管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Goods;


use App\Admin\Actions\Goods\GoodsBatchOffShelf;
use App\Admin\Actions\Goods\GoodsBatchOnShelf;
use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class GoodsController extends Controller
{
    use ModelForm;

    protected $header = '网点商品管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function grid(){

        $grid = new Grid(new GoodsModel());

        $grid->model()->orderBy('created_at', 'DESC');

//        $grid->column('id','商品编号')->display(function($filed){
//            return empty($filed) ? config('view.EmptyData') : $filed;
//        });

        $grid->column('goods_title','商品标题')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('goods_cover', '封面')->display(function ($filed) {
            return empty($filed) ? config('view.oss_zaiwu') : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('goods_banner','轮播图')->display(function($filed){
            return empty($filed) ? array() : $filed;
        })->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('network_id','网点')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : AdminUserModel::selectValue('id',$filed);
        });

        $grid->column('goods_type','类型')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : GoodsTypeModel::selectValue('id',$filed);
        });

        $grid->column('goods_specs','规格')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('retail_goods_price','上货价')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('goods_price','零售价')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('is_barrel_deposit','是否有桶押金')->using(DataDictionaryModel::Whether, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('barrel_deposit','桶押金')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('distribution_fee','奖励配送费')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

//        $grid->column('goods_stock','库存')->display(function($filed){
//            return empty($filed) ? 0 : $filed;
//        });

//        $grid->column('goods_sales','销量')->display(function($filed){
//            return empty($filed) ? 0 : $filed;
//        });

         $grid->column('weight','权重')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        })->sortable();

//        $grid->column('goods_delivery_type','配送类型')->using(DataDictionaryModel::GoodsDeliveryType, '未知')->dot([
//            0 => 'danger',
//            1 => 'info',
//        ], 'success');


        $grid->column('status','状态')->using(DataDictionaryModel::Status, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('goods_title','商品名称');
                $filter->like('goods_specs','商品规格');
                $filter->between('created_at', '创建时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('goods_type', '类型')->select(GoodsTypeModel::selectOptions());
                $filter->equal('network_id', '网点')->select(AdminUserModel::selectOptions());
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->disableCreateButton();
        $grid->disableCreateButton();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->tools(function ($tools) {
            $tools->append(new GoodsBatchOffShelf);
            $tools->append(new GoodsBatchOnShelf);
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new GoodsModel());

        $form->select('network_id', '所属网点')->options(AdminUserModel::selectOptions())->rules('required');

        $form->text('goods_title', '商品标题')->rules('required|max:150');

        $form->file('goods_cover','封面图')->uniqueName()->removable()->rules('required');

        $form->multipleImage('goods_banner','轮播图')->uniqueName()->removable()->help('多图上传请按住Ctrl键选择多张');

        $form->select('goods_type', '类型')->options(GoodsTypeModel::selectOptions())->rules('required');

        $form->text('goods_specs', '规格')->rules('required|max:150');

        $form->currency('retail_goods_price', '上货价')->symbol('￥');

//        $form->table('goods_price_data','价格', function ($table) {
//            $table->select('region_id', '区域')->options(RegionModel::selectOptions())->rules('required');
//            $table->currency('retail_goods_price', '上货价')->symbol('￥');
//            $table->currency('goods_price', '零售价')->symbol('￥');
//        });

        $form->currency('goods_price', '零售价')->symbol('￥');

        $form->currency('original_price', '划线价')->symbol('￥');

        $form->radio('is_barrel_deposit', '是否有桶押金')->options(DataDictionaryModel::Whether)->default('0');

        $form->currency('barrel_deposit', '桶押金')->symbol('￥')->help('如果有桶押金的话、押金金额必须填写');

        $form->currency('distribution_fee', '奖励配送费')->symbol('￥')->rules('required');

        $form->currency('freight', '运费')->symbol('￥')->rules('required');

//        $form->number('goods_stock', '库存');

        $form->number('weight', '权重')->help('数字越大排序越靠前');

        $form->radio('is_client_show', '是否在用户端销售')->options(DataDictionaryModel::Whether)->default('0');

//        $form->radio('goods_delivery_type', '配送类型')->options(DataDictionaryModel::GoodsDeliveryType)->default('0');

//        $form->radio('status', '状态')->options(DataDictionaryModel::GoodsStatus)->default('0');

        $form->editor('goods_detail','详情');


        $form->saved(function (Form $form) {

            $goods_id = $form->model()->id;

            NetworkGoodsModel::where(['goods_id'=>$goods_id])->update([
                'goods_title'=>$form->model()->goods_title,
                'goods_type'=>$form->model()->goods_type,
                'weight'=>$form->model()->weight,
            ]);

        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }


}
